package com.lechicanh.cgrid.ke.worker.worker.launcher;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.rmi.NotBoundException;
import java.rmi.UnmarshalException;
import java.util.logging.Logger;

import com.lechicanh.cgrid.ke.api.util.Util;
import com.lechicanh.cgrid.ke.worker.worker.WorkerManagement;

public class Launcher {
	private static final Logger m_logger = Logger.getLogger("Worker");
	public static void main(String[] args) {
		
		String server = Util.getParameter(args, "handler");
		String identifier = Util.getParameter(args, "name");
		if(server == null){
			System.out.println("Need to input -handler parameter");
			usage();
			return;
			
		}
		if(identifier == null){
			System.out.println("Need to input -name for worker");
			usage();
			return;
		}
		try {
			System.out.println("Start worker, remote hanlder:" + server +", name:" + identifier);
			WorkerManagement.start(server, identifier);
			
		}catch (UnmarshalException | ConnectException e2) { 
			System.out.println("Hanlder is shutting down");
			System.out.println("Closing worker");
			return;
		}
		catch (NotBoundException e) { 
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static final void usage(){
		System.out.println("Usage: java -jar worker.jar -handler [URL] -name [identifier]"); 
	}
}
